package com.zlz.server.controller;


import com.zlz.server.annotation.OperateLog;
import com.zlz.server.pojo.Admin;
import com.zlz.server.pojo.RespBean;
import com.zlz.server.pojo.Role;
import com.zlz.server.service.IAdminService;
import com.zlz.server.service.IRoleService;
import com.zlz.server.enums.OprLevel;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author efire
 * @since 2021-01-16
 */
@RestController
@RequestMapping("/system/admin")
public class AdminController {
    @Resource
    private IAdminService iAdminService;

    @Resource
    private IRoleService iRoleService;

    @ApiOperation("获取操作员")
    @GetMapping("/")
    public RespBean getAllOperates(String keywords) {
        return RespBean.success(iAdminService.getAllOperates(keywords));
    }

    @ApiOperation("更新操作员信息")
    @PutMapping("/")
    @OperateLog(operationName = "更新操作员信息", moduleName = "操作员", level = OprLevel.NORMAL)
    public RespBean updateAdmin(@RequestBody Admin admin) {
        if (iAdminService.updateById(admin)) {
            return RespBean.success("更新成功", null);
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation("删除操作员")
    @PutMapping("/{id}")
    @OperateLog(operationName = "删除操作员信息", moduleName = "操作员", level = OprLevel.HIGH)
    public RespBean delAdmin(@PathVariable("id") Long id) {
        if (iAdminService.removeById(id)) {
            return RespBean.success("删除成功", null);
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation("获取所有角色")
    @GetMapping("/roles")
    public RespBean getAllRoles() {
        List<Role> roles = iRoleService.list();
        return RespBean.success(roles);
    }

    @ApiOperation("更新操作员角色")
    @PutMapping("/role")
    @OperateLog(operationName = "更新操作员角色", moduleName = "操作员", level = OprLevel.NORMAL)
    public RespBean updateAdminRole(Long adminId, Long[] rids) {
        return iAdminService.updateAdminRole(adminId, rids);
    }

}
